#include <iostream>
#include <string>
#include <memory>
using std::cout;
using std::endl;
using std::unique_ptr;
using std::shared_ptr;
using std::string;

void test0(){
    string msg = "hello,world\n";
    FILE * fp = fopen("res1.txt","a+");
    fwrite(msg.c_str(),1,msg.size(),fp);
    fclose(fp);
}

struct FILECloser{
void operator()(FILE * fp){
  if(fp){
      fclose(fp);
      cout << "fclose(fp)" << endl;
  }
}
};

void test1(){
    string msg = "hello,world\n";
    unique_ptr<FILE,FILECloser> up(fopen("res2.txt","a+"));
    fwrite(msg.c_str(),1,msg.size(),up.get());
    /* fclose(up.get()); */
}

void test2(){
    string msg = "hello,world\n";
    FILECloser fc;
    shared_ptr<FILE> sp(fopen("res3.txt","a+"),fc);
    fwrite(msg.c_str(),1,msg.size(),sp.get());
    /* fclose(sp.get()); */
}

int main(void){
    test2();
    return 0;
}
